package wh.coach.pivot3.table.dynamic;

import wh.coach.domain.CalType;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author hangwen
 * @date 2024/2/23
 */
public class DataTableReader {

    public DataTable read(InputStream inputStream) {
        //我知道怎么读CSV文件，我清楚的知道我读到了哪一行哪一列
        //我知道第一行的数据表述row的属性
        //后续行就是数据了，一行一个DataRow
        DataTable dataTable = new DataTable();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            //读取第一行作为属性列表
            if ((line = reader.readLine()) != null) {
                String[] columns = line.split(",");
                dataTable.addColumns(columns);
            }
            //读取后续行作为数据并且创造DataRow对象
            while ((line = reader.readLine()) != null) {
                String[] values = line.split(",");
                dataTable.addRow(values);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return dataTable;

    }
}
